package com.im.app.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

/**
 * json工具类
 */
public class JsonUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();


    /**
     * 将对象转换成json字符串
     * @param obj
     * @return
     */
    public static String object2JsonStr(Object obj){
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将字符串转换成对象
     * @param jsonStr
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T jsonStr2object(String jsonStr, Class<T> clazz){
        try {
            return objectMapper.readValue(jsonStr.getBytes("UTF-8"),clazz);
        } catch (JsonParseException e){
            e.printStackTrace();
        }catch (JsonMappingException e){
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     * @param jsonStr
     * @param beanType
     * @param <T>
     * @return
     */
    public static <T>List<T> jsonToList(String jsonStr, Class<T> beanType){
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = objectMapper.readValue(jsonStr,javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 将byte数组转换成object对象
     * @param bytes
     * @return
     */
    public static Object byteArrayToJsonObject(byte[] bytes){
        String str=new String(bytes);
        return JSONObject.parse(str);
    }




}